package com.leinbo.onedoc.utils

import com.aspose.pdf.Document
import com.aspose.pdf.HorizontalAlignment
import com.aspose.pdf.VerticalAlignment
import com.aspose.pdf.WatermarkArtifact
import com.aspose.pdf.facades.EncodingType
import com.aspose.pdf.facades.FontStyle
import com.aspose.pdf.facades.FormattedText
import com.kotoframework.onedoc.utils.TransformUtil.excel2Htm
import com.kotoframework.onedoc.utils.TransformUtil.excel2Pdf
import com.kotoframework.onedoc.utils.TransformUtil.pdf2Jpg
import com.kotoframework.onedoc.utils.TransformUtil.ppt2Pdf
import com.kotoframework.onedoc.utils.TransformUtil.word2Pdf
import com.leinbo.onedoc.kpojo.Config
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
import java.io.File
import javax.servlet.http.HttpServletRequest

/**
 * Created by sundaiyue on 2022/11/23 15:49
 */
object CommonUtil {
    // 在启动时从config表中读取配置信息
    var globalConfig: Config = Config()

    fun getRequestAddress(request: HttpServletRequest): String {
        return if (request.serverName == "localhost" || request.serverName == "127.0.0.1" || request.serverName == "0.0.0.0" || request.serverName.startsWith(
                "192.168"
            )
        ) {
            "http://${request.serverName}:${request.serverPort}"
        } else {
            "https://${request.serverName}"
        }
    }

    fun transform2Pdf(file: File): Pair<String, String> {
        val pdfPath = when (file.extension) {
            "doc", "docx" -> word2Pdf(file)
            "xls", "xlsx" -> excel2Pdf(file)
            "ppt", "pptx" -> ppt2Pdf(file)
            else -> file.absolutePath
        }

        return pdfPath to pdf2Jpg(File(pdfPath))
    }

    fun addWaterMark(
        pdfPath: String,
        config: Config.Companion.WatermarkConfig,
    ) {
        val document = Document(File(pdfPath).inputStream())
        val formattedText =
            FormattedText(
                config.text,
                config.fontColor,
                FontStyle.TimesBold,
                EncodingType.Identity_h,
                true,
                config.fontSize?.toFloat() ?: 20f
            )

        document.pages.forEach {
            val artifact = WatermarkArtifact()
            artifact.setText(formattedText)
            artifact.artifactHorizontalAlignment = config.horizontalAlign ?: HorizontalAlignment.Center
            artifact.artifactVerticalAlignment = config.verticalAlign ?: VerticalAlignment.Center
            artifact.rotation = config.rotation ?: 0.0
            artifact.opacity = config.opacity ?: 0.5
            artifact.isBackground = true
            it.artifacts.add(artifact)
        }

        document.save(pdfPath)
        document.close()
    }

    fun transform2Htm(file: File): String {
        return when (file.extension) {
            "xls", "xlsx" -> excel2Htm(file)
            else -> {
                ""
            }
        }
    }

    val activeRequest: HttpServletRequest
        get() {
            return (RequestContextHolder.getRequestAttributes() as ServletRequestAttributes?)!!.request
        }
}
